package com.kfm.base.io.o;

import java.io.*;

public class Demo03 {

    public static void main(String[] args) {
        /*
        序列化后反序列化的对象和原对象是同一个吗？
         */

        try(ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("text/serializable1.txt"));
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("text/serializable1.txt"))
        ){
//            Student stu = new Student("秦爽", 59.5, "男");
//
//            oos.writeObject(stu);
//
//            Object o = ois.readObject();
//            System.out.println(o);
//
//            System.out.println(stu == o);
            Singleton instance = Singleton.getInstance();

            oos.writeObject(instance);

            Object o = ois.readObject();

            System.out.println(o);
            System.out.println(o == instance);


        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}

class Singleton {

    private static Singleton instance;

    private Singleton(){
//        if (instance != null){
//            throw new RuntimeException("单例");
//        }
    }

    public static Singleton getInstance(){
        if (instance == null){
            instance = new Singleton();
        }
        return instance;
    }
}
